package com.happyhome.services;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.happyhome.constants.ComTxManagerName;
import com.happyhome.dao.NewsDao;
import com.happyhome.datamodel.News;

@Service
public class NewsServiceImpl implements NewsService{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(NewsServiceImpl.class);
	
	@Autowired
    private NewsDao newsDao;

	@Override
	public News getNewsById(Long id) {
		return newsDao.find(id);
	}

	@Override
	public List<News> getAllNews() {
		return newsDao.getAllNews();
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public News merge(News news) {
		return newsDao.merge(news);
	}

	@Override
	public News findNesById(Long Id) {
		return newsDao.find(Id);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void setDisplayCavas(List<Long> listNews) {
		List<News> listNewsInList = newsDao.getListNewsInListId(listNews);
		for(News newsIn : listNewsInList){
			newsIn.setIsDisplay(Boolean.valueOf("true"));
		}
		
		List<News> listNewsOutList = newsDao.getListNewsNotInListId(listNews);
		for(News newsOut : listNewsOutList){
			newsOut.setIsDisplay(Boolean.valueOf("false"));
		}
		newsDao.flush();
	}

	@Override
	public List<News> getListNewsIsDisplay() {
		return newsDao.getListNewsIsDisplay();
	}
}
